package com.wang.tool.proxy;

import com.alibaba.fastjson.JSONObject;

/**
 * 后置+异常切面
 *
 * @Author wangshaopeng
 * @Date 2020/8/5
 */
public abstract class AfterCut implements Cut {


    @Override
    public final Object invoke(FunctionProxy<Object, Object>.InvokeChain chain, Object param, JSONObject runCtx) {
        Object result = null;
        RuntimeException e = null;
        try {
            return result = chain.chain(param, runCtx);
        } catch (RuntimeException err) {
            e = err;
            throw e;
        } finally {
            after(param, runCtx, result, e);
        }
    }

    /**
     * 后置操作
     *
     * @param param  入参
     * @param runCtx 执行环境 非空
     * @param result 执行结果
     * @param e      出现的异常
     */
    protected abstract void after(Object param, JSONObject runCtx, Object result, RuntimeException e);


    @Override
    public final int level() {
        return 3;
    }

}
